package com.smz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smz.dao.CommodityDAO;
import com.smz.entity.Commodity;
import com.smz.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityDAO, Commodity> implements CommodityService {

    @Autowired
    private CommodityDAO commodityDAO;

    @Override
    public Commodity findCommodityById(Integer id) {
        return commodityDAO.selectCommodityById(id);
    }

    @Override
    public List<Commodity> findAllCommodity() {
        return commodityDAO.selectAllCommodity();
    }

    //dy
    public Page<Commodity> findPageCommodity(Integer current, Integer rows, String name) {
        long count = commodityDAO.selectCount(null);
        Page<Commodity> page = new Page<>(current, rows, count);
        Page<Commodity> commodityPage = commodityDAO.selectPage(page, null);
        return commodityPage;
    }

    public List<Commodity> selectCommodity(String pName) {
        return commodityDAO.selectCommodity(pName);
    }

    @Override
    public Page<Commodity> findPageCommodityByWy(Integer current, Integer rows, String name) {
        long count = commodityDAO.selectCount(null);
        Page<Commodity> page = new Page<>(current, rows, count);
        if(name == null){
            Page<Commodity> commodityPage = commodityDAO.selectPage(page, null);
            return commodityPage;
        }else {
            Page<Commodity> commodityPage = commodityDAO.selectPage(page, new QueryWrapper<Commodity>().lambda().like(Commodity::getPName, name));
            return commodityPage;
        }
    }

    @Override
    public void addCommodity(Commodity commodity) {
        commodityDAO.insert(commodity);
    }

    @Override
    public void removeCommodityById(Integer id) {
        commodityDAO.deleteById(id);
    }

    @Override
    public void modifyCommodity(Commodity commodity) {
        commodityDAO.updateById(commodity);
    }

}
